package com.zzl.util;

import com.zzl.entity.User;
import com.zzl.services.UserService;
import com.zzl.services.impl.UserServicesImpl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Date;

public class DbUtil {
    //数据库
    private String dbUrl = "jdbc:mysql://localhost:3306/student_system";
    //用户名
    private String dbUser = "root";
    //密码
    private String dbPassword = "123456";
    //mysql驱动
    private String jdbcName = "com.mysql.cj.jdbc.Driver";

    public Connection getCon() throws Exception{
        //加载驱动
        Class.forName(jdbcName);
        //获取连接
        Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
        //调用getCon()得到数据库连接con
        return con;
    }

    public void closeCon(Connection con) throws Exception{
        //如果连接不为空，关闭
        if(con != null){
            con.close();
        }
    }


    public static void main(String[] args) {
        //获得数据库工具类对象
        DbUtil dbUtil = new DbUtil();
        UserService userServices = new UserServicesImpl();
        User user = new User();
        user.setUserName("su");
        user.setPassword("123456");
        try {
            dbUtil.getCon();
            User resultUser = userServices.login(dbUtil.getCon(), user);
            System.out.println(resultUser);
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
    }
}
